package com.wolfeyes.common.utils.network.web;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author yanyljava
 *
 */
public class BeanUtils {
	/**
	 * Bean转换成Map键值对中值是空串设置成NULL
	 * 
	 * @param bean
	 * @return
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public static Map<String, Object> bean2Map(Object bean)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		Map<String, Object> tmpMap = new HashMap<String, Object>();
		tmpMap.putAll(PropertyUtils.describe(bean));
		for (Entry<String, Object> map : tmpMap.entrySet()) {
			Object value = map.getValue();
			if (value != null) {
				if (String.class == value.getClass()) {
					if (StringUtils.isBlank((String) value)) {
						tmpMap.put(map.getKey(), null);
					}
				}
			}

		}

		return tmpMap;
	}
}
